package cn.cxq.learning.sort;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

public class Client {

    @Test
    public void testBubbleSort(){

        System.out.println("排序过程：");
//      BubbleSort.sort(arr);
        BubbleSort.optimizeSort(initIntArray(10));

        System.out.println("100000个数据测试：");

        long pre = System.currentTimeMillis();

        BubbleSort.sort(initIntArray(100000));

        long post = System.currentTimeMillis();

        System.out.println("排序用时：" + (post - pre) +" ms");
    }

    @Test
    public void testSelectSort(){
        System.out.println("排序过程：");

        SelectSort.printSort(initIntArray(10));

        System.out.println("100000个数据测试：");

        long pre = System.currentTimeMillis();

        SelectSort.sort(initIntArray(100000));

        long post = System.currentTimeMillis();

        System.out.println("排序用时：" + (post - pre) +" ms");
    }

    @Test
    public void testInsertSort(){
        System.out.println("排序过程：");

        InsertSort.printSort(initIntArray(10));

        System.out.println("100000个数据测试：");

        long pre = System.currentTimeMillis();

        InsertSort.sort(initIntArray(100000));

        long post = System.currentTimeMillis();

        System.out.println("排序用时：" + (post - pre) +" ms");
    }


    @Test
    public void testShellSort(){

        System.out.println("交换法：");
        System.out.println("排序过程：");

        ShellSort.printSort(initIntArray(10));

        System.out.println("100000个数据测试：");


        long pre = System.currentTimeMillis();

        ShellSort.sort(initIntArray(100000));

        long post = System.currentTimeMillis();


        // 通过测试可得：这个交换法并没有达到优化的目的，反而比原来的插入排序更慢了。
        System.out.println("排序用时：" + (post - pre) +" ms");

        System.out.println("----------------------------------");

        System.out.println("移位法：");
        System.out.println("排序过程：");

        ShellSort.printSort2(initIntArray(10));

        System.out.println("100000个数据测试：");

        long pre1 = System.currentTimeMillis();

        ShellSort.sort2(initIntArray(100000));

        long post1 = System.currentTimeMillis();

        // 使用移位法，速度可谓质的飞跃。
        System.out.println("排序用时：" + (post1 - pre1) +" ms");
    }

    @Test
    public void testQuickSort(){

        System.out.println("选择中间作为基准数");

        System.out.println("排序过程：");
        int[] arr = initIntArray(10);
//        int[] arr = {15, 72, 73, 15, 15, 89, 77, 93, 59, 73};
        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(QuickSort.printSort(arr, 0, arr.length - 1)));

        System.out.println("100000个数据测试：");

        long pre1 = System.currentTimeMillis();

        int[] arr2 = initIntArray(100000);

        QuickSort.sort(arr2, 0, arr2.length-1);

        long post1 = System.currentTimeMillis();


        System.out.println("排序用时：" + (post1 - pre1) +" ms");

        System.out.println("选择首位作为基准数");

        System.out.println("排序过程：");
        int[] arr3 = initIntArray(10);
//        int[] arr3 = {15, 72, 73, 15, 15, 89, 77, 93, 59, 73};
        System.out.println(Arrays.toString(arr3));
        System.out.println("最终结果：\n" + Arrays.toString(QuickSort.printSort2(arr3, 0, arr3.length - 1)));

        System.out.println("100000个数据测试：");

        long pre2 = System.currentTimeMillis();

        int[] arr4 = initIntArray(100000);

        QuickSort.sort2(arr4, 0, arr4.length - 1);

        long post2 = System.currentTimeMillis();

        System.out.println("排序用时：" + (post2 - pre2) +" ms");
    }

    public int[] initIntArray(int num){
        int[] arr = new int[num];
        for (int i = 0; i < arr.length; i++) arr[i] = (int) (Math.random()*num*10);
        return arr;
    }

    @Test
    public void testMergeSort(){
        System.out.println("排序过程：");
        int[] arr = initIntArray(10);

        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(MergeSort.printSort(arr, 0, arr.length - 1, arr.clone())));

        System.out.println("100000个数据测试：");

        long pre2 = System.currentTimeMillis();

        int[] arr2 = initIntArray(100000);

        MergeSort.sort(arr2, 0, arr2.length - 1,arr2.clone());

        long post2 = System.currentTimeMillis();

        System.out.println("排序用时：" + (post2 - pre2) +" ms");
    }

    @Test
    public void testCardinalSort(){
        System.out.println("排序过程：");
        int[] arr = initIntArray(10);

        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(CardinalSort.printSort(arr)));

        System.out.println("100000个数据测试：");

        long pre2 = System.currentTimeMillis();

        int[] arr2 = initIntArray(100000);

        CardinalSort.sort(arr2);

        long post2 = System.currentTimeMillis();

        System.out.println("排序用时：" + (post2 - pre2) +" ms");
    }

    @Test
    public void testHeapSort(){
        System.out.println("排序过程：");
        int[] arr = initIntArray(10);
//        int[] arr = {4,6,8,5,9};

        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(HeapSort.printSort(arr)));

        System.out.println("100000个数据测试：");

        long pre2 = System.currentTimeMillis();

        int[] arr2 = initIntArray(100000);

        HeapSort.sort(arr2);

        long post2 = System.currentTimeMillis();

        System.out.println("排序用时：" + (post2 - pre2) +" ms");
    }
}
